﻿using BusinessLogic.Audit;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MailWindowsService
{
    public partial class ServiceMail : ServiceBase
    {
        #region declare
        protected Thread m_thread;
        //protected Thread m_threadSend;

        protected ManualResetEvent m_shutdownEvent;
        //protected ManualResetEvent m_shutdownEventSend;
        protected TimeSpan m_delay;
        //protected TimeSpan m_delaySend;
        //private static System.Timers.Timer _aTimer;
        #endregion
        public ServiceMail()
        {
            InitializeComponent();
            // create a new timespan object with a default of 10 seconds delay.
            m_delay = new TimeSpan(0, 0, 0, 10, 0);
            //m_delaySend = new TimeSpan(0, 0, 0, 10, 0);

        }
        #region
        #endregion
        public void Start(string[] args)
        {
            this.OnStart(args);
        }
        public void Stop()
        {
            this.OnStop();
        }
        protected override void OnStart(string[] args)
        {
            // create our threadstart object to wrap our delegate method
            ThreadStart ts = new ThreadStart(this.ServiceMain);

            // create the manual reset event and set it to an initial state of unsignaled
            m_shutdownEvent = new ManualResetEvent(false);

            // create the worker thread
            m_thread = new Thread(ts);

            // go ahead and start the worker thread
            m_thread.Start();
            #region test sendmail
            //ThreadStart tsSend = new ThreadStart(this.ServiceMain);
            //m_shutdownEventSend = new ManualResetEvent(false);
            //m_threadSend = new Thread(tsSend);
            //m_threadSend.Start();
            #endregion

            // call the base class so it has a chance to perform any work it needs to
            base.OnStart(args);
        }

        protected override void OnStop()
        {
            // signal the event to shutdown
            m_shutdownEvent.Set();

            // wait for the thread to stop giving it 10 seconds
            m_thread.Join(10000);

            // signal the event to shutdown

            // call the base class 
            base.OnStop();
        }
        /// <summary>
        /// 
        /// </summary>
        protected void ServiceMain()
        {
            bool bSignaled = false;
            int nReturnCode = 0;

            while (true)
            {
                // wait for the event to be signaled or for the configured delay
                bSignaled = m_shutdownEvent.WaitOne(m_delay, true);

                // if we were signaled to shutdow, exit the loop
                if (bSignaled == true)
                    break;

                // let's do some work
                nReturnCode = Execute();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected virtual int Execute()
        {
            // for right now we'll just log a message in the Application message log to let us know that
            // our service is working
            MainService autoMessage = new MainService();
            //autoMessage.LogService.InsertLog(LogTypeEnum.MailError, "Start " + ServiceName + " ::Execute()", DateTime.Now.ToString());
            autoMessage.Execute();
            //autoMessage.LogService.InsertLog(LogTypeEnum.MailError, "Stop Receive mail", DateTime.Now.ToString());
            return 1;
        }
    }
}
